<?php

class Admin_NewscatController extends Zend_Controller_Action
{	
    private $_params;
	private $_auth;
	private $_acl;
    public function init(){
        $this->_params      = $this->_request->getParams();
        
        $this->_acl         = new My_Acl();
        $this->view->acl    = $this->_acl;
        
        $this->_auth        = $this->_acl->checkAllowed();
        $this->view->auth   = $this->_auth;
    }
    public function indexAction(){
        $modelNewscat            = new Admin_Model_Newscat();
        $newsCatList             = $modelNewscat->getAllData()->toArray();        
        $this->view->newsCatList = $newsCatList;
    }
    public function addAction(){   
        $form = new Admin_Form_Newscat();
        $form->submit->setLabel('Thêm mới');
        $this->view->form = $form;
        if($this->getRequest()->isPost()){
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)){
                $addData['catName']     = $form->getValue('catName');
                $addData['catCode']     = $form->getValue('catCode');
                $addData['display']      = $form->getValue('display');
                $addData['ord']         = $form->getValue('ord');
                $addData['shortName']   = My_Service_String::shortenString($form->getValue('newscatname'));

                $modelNewsCat = new Admin_Model_Newscat();
                $newsCatCheck = $modelNewsCat->getDataCheckUnique(array('catName'=>$addData['catName'],'catCode'=>$addData['catCode']));                
                if(!$newsCatCheck){
                    $newsCatId = $modelNewsCat->addData($addData);
                    if($newsCatId){
                        $this->_helper->redirector('index', 'newscat', 'admin');
                    }else{
                        $this->view->mess = "Đã có lỗi xảy ra. Vui lòng thử lại!";
                        $this->view->flag = "error";
                   }
                }else{
                    $this->view->mess = "Tên hoặc Mã danh mục đã tồn tại. Vui lòng chọn tên khác!";
                    $this->view->flag = "error"; 
                }
            }
        }
    }
    public function editAction(){
        $newsCatId = (int)($this->_getParam('newsCatId', 0));
        $modelNewsCat = new Admin_Model_Newscat();
        $newsCatCheck = $modelNewsCat->getDataByCatId($newsCatId);
        $form = new Admin_Form_Newscat();
        $this->view->form = $form;
        if($newsCatCheck){
            if($this->getRequest()->isPost()){
                $formData = $this->getRequest()->getPost();
                if ($form->isValid($formData)){
                    $editData['catId']     = $form->getValue('catId');
                    $editData['catName']     = $form->getValue('catName');
                    $editData['catCode']     = $form->getValue('catCode');
                    $editData['display']      = $form->getValue('display');
                    $editData['ord']         = $form->getValue('ord');
                    $editData['shortName']   = My_Service_String::shortenString($form->getValue('catName'));
    
                    $modelNewsCat = new Admin_Model_Newscat();
                    $newsCatCheck = $modelNewsCat->getDataCheckUnique(array('catName'=>$editData['catName'],'catCode'=>$editData['catCode']));                         
                    if(!$newsCatCheck || $editData['catId'] == $newsCatCheck['catId']){
                        $modelNewsCat->updateData($editData['catId'],$editData);
                        $this->_helper->redirector('index', 'newscat', 'admin');
                    }else{
                        $this->view->mess = "Tên hoặc Mã danh mục đã tồn tại. Vui lòng chọn tên khác!";
                        $this->view->flag = "error";
                    }
                }
            }else{                
                $form->populate($newsCatCheck->toArray());
            }
        }else{
            $this->_helper->redirector('index', 'newscat', 'admin');
        }
    }
    public function deleteAction(){
        if($this->getRequest()->isPost()){
            $del = $this->getRequest()->getPost('del');
            if($del == 'Có'){               
                $newsCatId = $this->getRequest()->getPost('newsCatId');
                $modelNewsCat = new Admin_Model_Newscat();
                $modelNewsCat->deleteData($newsCatId);
                // ghi log
            }
            $this->_helper->redirector('index', 'newscat', 'admin');
        }else{
            $id = $this->_getParam('newsCatId', 0);
            $modelNewsCat = new Admin_Model_Newscat();
            $this->view->newsCatCheck = $modelNewsCat->getDataByCatId($id);
        }
    }
}